//package com.hebeiwangxiaozhang.gateway.service.oauth;
//
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.security.authorization.AuthorizationDecision;
//import org.springframework.security.authorization.ReactiveAuthorizationManager;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.oauth2.provider.OAuth2Authentication;
//import org.springframework.security.web.server.authorization.AuthorizationContext;
//import org.springframework.stereotype.Component;
//import org.springframework.util.AntPathMatcher;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//import java.util.Set;
//import java.util.concurrent.ConcurrentSkipListSet;
//
//@Component
//public class AccessManager implements ReactiveAuthorizationManager<AuthorizationContext> {
//    private Set<String> permitAll = new ConcurrentSkipListSet<>();
//    private static final AntPathMatcher antPathMatcher = new AntPathMatcher();
//
//
//    public AccessManager() {
//        permitAll.add("/");
//        permitAll.add("/error");
//        permitAll.add("/favicon.ico");
//        permitAll.add("/**/v2/api-docs/**");
//        permitAll.add("/**/swagger-resources/**");
//        permitAll.add("/webjars/**");
//        permitAll.add("/doc.html");
//        permitAll.add("/swagger-ui.html");
//        permitAll.add("/**/oauth/**");
//        permitAll.add("/**/current/get");
//    }
//
//    @Override
//    public Mono<AuthorizationDecision> check(Mono<Authentication> authenticationMono
//            , AuthorizationContext authorizationContext) {
//        ServerWebExchange exchange = authorizationContext.getExchange();
//        return authenticationMono.map(auth -> {
//            return new AuthorizationDecision(checkAuthorities(exchange, auth));
//        }).defaultIfEmpty(new AuthorizationDecision(false));
//
//    }
//
//    private boolean permitAll(String requestPath) {
//        return permitAll.stream()
//                .filter(r -> antPathMatcher.match(r, requestPath)).findFirst().isPresent();
//    }
//
//    private boolean checkAuthorities(ServerWebExchange exchange, Authentication auth) {
//        String requestPath = exchange.getRequest().getURI().getPath();
//        if (permitAll(requestPath)) {
//            return true;
//        }
//        if (auth instanceof OAuth2Authentication) {
//            OAuth2Authentication athentication = (OAuth2Authentication) auth;
//            String clientId = athentication.getOAuth2Request().getClientId();
//            if(StringUtils.isNotEmpty(clientId)) {
//                return true;
//            }
//        }
//        return false;
//    }
//}